//
//  ShakeController.swift
//  FunSeeker
//
//  Created by 王泽宇 on 2017/3/26.
//  Copyright © 2017年 edu.Tongji.sse. All rights reserved.
//

import UIKit

class ShakeController: UIViewController, UIViewControllerTransitioningDelegate, MoreControllerDelegate {
    var shakeBGView : UIImageView?
    
    var text = ""
    var actName = ""
    
    @IBOutlet weak var itemText: UITextField!
    @IBOutlet var itemButtons: [UIButton]!
    
    var items = ["美食","饮品","电影院"]
    
    @IBOutlet weak var readyButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        // Do any additional setup after loading the view.
        readyButton.setTitle("选好了", for: .normal)
        readyButton.setTitle("选择一个项目", for: .disabled)
        readyButton.isEnabled = false
    }
    
    func getChosenItem(text: String, actName: String) {
        deactiveAll()
        self.actName = actName
        self.text = text
        itemText.text = text
        readyButton.isEnabled = true
    }
    
    @IBAction func clickItem(_ sender: UIButton) {
//        itemText.text = items[itemButtons.index(of: sender)!/2]
        getChosenItem(text: items[itemButtons.index(of: sender)!/2], actName: "tbd")
        deactiveAll()
        let picButton = itemButtons[itemButtons.index(of: sender)!/2*2]
        
        self.text = itemText.text!
        switch itemButtons.index(of: picButton)! as Int {
        case 0:
            picButton.setImage(UIImage(named: "food_active"), for: .normal)
            self.actName = "food"
            break
        case 2:
            picButton.setImage(UIImage(named: "drink_active"), for: .normal)
            self.actName = "drink"
            break
        case 4:
            picButton.setImage(UIImage(named: "movie_active"), for: .normal)
            self.actName = "movie"
            break
        default:
            break
        }
    }
    
    func deactiveAll() {
        itemButtons[0].setImage(UIImage(named: "food"), for: .normal)
        itemButtons[2].setImage(UIImage(named: "drink"), for: .normal)
        itemButtons[4].setImage(UIImage(named: "movie"), for: .normal)
    }
    
    func setupUI() {
        let back = UIBarButtonItem(title: "", style: .done, target: nil, action: nil)
        self.navigationItem.backBarButtonItem = back
        self.navigationItem.title = "摇一摇"
        let sbg = UIImage(named: "shake_bg")
        shakeBGView = UIImageView(frame: CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width*(sbg?.size.height)!/(sbg?.size.width)!))
        shakeBGView?.contentMode = .scaleAspectFit
        shakeBGView?.image = sbg
        view.addSubview(shakeBGView!)
        view.sendSubview(toBack: shakeBGView!)
    }
    
    @IBAction func more(_ sender: UIButton) {
        let moreVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MoreController") as! MoreController
        moreVC.delegate = self
        moreVC.transitioningDelegate = self
        moreVC.modalPresentationStyle = .custom
        self.present(moreVC, animated: true, completion: nil)
    }
    
    @IBAction func ready(_ sender: UIButton) {
        let shakeActionVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ShakeActionController") as! ShakeActionController
        shakeActionVC.text = self.text
        shakeActionVC.actName = self.actName
        self.navigationController?.pushViewController(shakeActionVC, animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

    //MARK: - UIViewControllerTransitioningDelegate
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CustomerPresentingAnimator()
    }
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CustomerDismissingAnimator()
    }

}
